package com.fingard.dsp.bank.directbank.citicunion01;

import java.util.HashMap;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;

public class CITICUnion01OtherElse extends CITICUnion01Base implements IOtherElse {
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		if ("VerifySign".equals(p_reqElse.reqHeader.transType)) {
			return verifySign(p_reqElse);
		}   else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + p_reqElse.reqHeader.transType);
			return retAts;
		}
	}

	public RetAtsOtherElse verifySign(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();
		try {
			String ownAct = "";
			if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
				ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
			}
			String merchantId = "";
			if (p_reqElse.reqHashMap.containsKey("MchId")) {
				merchantId = p_reqElse.reqHashMap.get("MchId");
			}
			String appId = "";
			if (p_reqElse.reqHashMap.containsKey("AppId")) {
				appId = p_reqElse.reqHashMap.get("AppId");
			}

			String plain = "";
			if (p_reqElse.reqHashMap.containsKey("Plain")) {
				plain = p_reqElse.reqHashMap.get("Plain");
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "没有原文参数");
				return retAts;
			}

			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(ownAct, merchantId, appId, "unifiedorder");
			if (actItem != null) {
				StringBuilder tmpSb = new StringBuilder();
				if (verifySign(plain, actItem.oppCertFilePath, tmpSb)) {
					retAts.retHashMap.put("RespCode", "0");
					retAts.retHashMap.put("RespInfo", "验签通过");
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", tmpSb.toString());
				}
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
